//
//  WCNewTextFileProcessor.m
//  WabbitCode
//
//  Created by William Towe on 1/15/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCNewTextFileProcessor.h"
#import "NSString+WCExtensions.h"


#define kWCNewTextFileProcessorFileNamePlaceholder @"<<FILE_NAME>>"
#define kWCNewTextFileProcessorProjectNamePlaceholder @"<<PROJECT_NAME>>"
#define kWCNewTextFileProcessorFullUserNamePlaceholder @"<<FULL_USER_NAME>>"
#define kWCNewTextFileProcessorShortUserNamePlaceholder @"<<SHORT_USER_NAME>>"
#define kWCNewTextFileProcessorFullDatePlaceholder @"<<FULL_DATE>>"
#define kWCNewTextFileProcessorYearPlaceholder @"<<YEAR>>"

NSString *WCNewTextFileProcessorFileNameKey = @"WCNewTextFileProcessorFileNameKey";
NSString *WCNewTextFileProcessorProjectNameKey = @"WCNewTextFileProcessorProjectNameKey";

@implementation WCNewTextFileProcessor
+ (WCNewTextFileProcessor *)sharedInstance; {
	static id _sharedInstance = nil;
	if (!_sharedInstance) {
		_sharedInstance = [[[self class] alloc] init];
	}
	return _sharedInstance;
}

- (NSString *)stringByReplacingPlaceholdersInString:(NSString *)string withAttributes:(NSDictionary *)attributes; {
	NSMutableString *copy = [[string mutableCopy] autorelease];
	
	// file name
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorFileNamePlaceholder withString:[attributes objectForKey:WCNewTextFileProcessorFileNameKey] options:NSLiteralSearch range:[copy wholeRange]];
	// project name
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorProjectNamePlaceholder withString:[attributes objectForKey:WCNewTextFileProcessorProjectNameKey] options:NSLiteralSearch range:[copy wholeRange]];
	// full user name
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorFullUserNamePlaceholder withString:NSFullUserName() options:NSLiteralSearch range:[copy wholeRange]];
	// short user name
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorShortUserNamePlaceholder withString:NSUserName() options:NSLiteralSearch range:[copy wholeRange]];
	
	// full date
	NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] initWithDateFormat:@"%m/%d/%Y" allowNaturalLanguage:NO] autorelease];
	
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorFullDatePlaceholder withString:[dateFormatter stringFromDate:[NSDate date]] options:NSLiteralSearch range:[copy wholeRange]];
	
	// year
	dateFormatter = [[[NSDateFormatter alloc] initWithDateFormat:@"%Y" allowNaturalLanguage:NO] autorelease];
	[copy replaceOccurrencesOfString:kWCNewTextFileProcessorYearPlaceholder withString:[dateFormatter stringFromDate:[NSDate date]] options:NSLiteralSearch range:[copy wholeRange]];
	
	return [[copy copy] autorelease];
}

- (NSData *)dataByReplacingPlaceholdersInString:(NSString *)string usingEncoding:(NSStringEncoding)encoding withAttributes:(NSDictionary *)attributes; {
	return [[self stringByReplacingPlaceholdersInString:string withAttributes:attributes] dataUsingEncoding:encoding];
}
@end
